package com.lzy.webapi.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.dtos.agent.AgentProtocolDto;
import com.lzy.product.dtos.agent.AgentProtocolInfoDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.AgentProtocolFacade;
import com.lzy.webapi.request.AgentProtocolRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/agentProtocol")
public class AgentProtocolController extends BaseController {

    @Autowired
    private AgentProtocolFacade agentProtocolFacade;

    /**
     * 协议详情
     *
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result createOrder(@RequestHeader("token") String token ,@RequestParam(value = "id")Long id) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        Result result = new Result();
        AgentProtocolInfoDto agentProtocolInfoDto = agentProtocolFacade.getAgentProtocolInfo(id,ut.getUserId());
        result.setResult(agentProtocolInfoDto);
        return result;
    }

    /**
     * 协议列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result agentProtocolList(@RequestHeader("token") String token ,AgentProtocolRequest request) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<AgentProtocolDto> pageInfo = agentProtocolFacade.agentProtocolList(pager,ut.getUserId());
        if (pageInfo != null) {
            return new Result(pageInfo.getList());
        }
        return null;
    }

    /**
     * 获取用户销售的产品
     *
     * @return
     */
    @RequestMapping(value = "/getProduct", method = RequestMethod.POST)
    @ResponseBody
    public Result getProduct(@RequestParam(value = "id")Long id,@RequestParam(value = "type")Integer type) {
        Map map = agentProtocolFacade.getProduct(id,type);
        return new Result(map);
    }

}
